#include "adminsys.h"
#include "ui_adminsys.h"

AdminSYS::AdminSYS(QString username, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdminSYS)
{
    ui->setupUi(this);
    this->username = username;
    client = new Client();
    //admin = new Admin(username);

    modelInit();
    connect(ui->searchBtn, &QPushButton::clicked, this, &AdminSYS::search);
    connect(ui->refreshBtn, &QPushButton::clicked, this, &AdminSYS::refreshInfo);
    connect(ui->addCourierBtn, &QPushButton::clicked, this, &AdminSYS::addCourier);
    connect(ui->deleteCourierBtn, &QPushButton::clicked, this, &AdminSYS::deleteCourier);
    connect(ui->assignBtn, &QPushButton::clicked, this, &AdminSYS::assignTask);
    connect(ui->switchBtn, &QPushButton::clicked, [=](){
        static int i = 1;
        ui->stackedWidget->setCurrentIndex((i++)  % 3);
    });
}

AdminSYS::~AdminSYS()
{
    delete ui;
}

void AdminSYS::modelInit()
{
    model = new QSqlTableModel(this);
    model->setTable("expressinfo");
    model->setHeaderData(0, Qt::Horizontal, "订单号");
    model->setHeaderData(1, Qt::Horizontal, "物品类型");
    model->setHeaderData(2, Qt::Horizontal, "寄件人");
    model->setHeaderData(3, Qt::Horizontal, "寄件时间");
    model->setHeaderData(4, Qt::Horizontal, "收件人");
    model->setHeaderData(5, Qt::Horizontal, "快递员");
    model->setHeaderData(6, Qt::Horizontal, "物流状态");
    model->setHeaderData(7, Qt::Horizontal, "收件时间");
    model->setHeaderData(8, Qt::Horizontal, "价格");
    model->select();
    ui->tableView->setModel(model);

    model2 = new QSqlTableModel(this);
    model2->setTable("userinfo");
    model2->setFilter("usertype = '快递员'");
    model2->setHeaderData(0, Qt::Horizontal, "用户名");
    model2->setHeaderData(1, Qt::Horizontal, "姓名");
    model2->setHeaderData(2, Qt::Horizontal, "电话");
    model2->setHeaderData(3, Qt::Horizontal, "余额");
    model2->removeColumn(4);    // 移除住址和类型
    model2->removeColumn(4);
    model2->select();
    ui->tableView_2->setModel(model2);

    model3 = new QSqlTableModel(this);
    model3->setTable("userinfo");
    model3->setFilter("usertype = '用户'");
    model3->setHeaderData(0, Qt::Horizontal, "用户名");
    model3->setHeaderData(1, Qt::Horizontal, "姓名");
    model3->setHeaderData(2, Qt::Horizontal, "电话");
    model3->setHeaderData(3, Qt::Horizontal, "余额");
    model3->setHeaderData(3, Qt::Horizontal, "地址");
    model3->removeColumn(5);
    model3->select();
    ui->tableView_3->setModel(model3);
}

void AdminSYS::search()
{
    int i = ui->searchTypeBox->currentIndex();
    QString arg = ui->searchEdit->text();
    if (i == 0)
        model->setFilter(QString("sender = '%1'").arg(arg));
    else if (i == 1)
        model->setFilter(QString("sendTime = '%1'").arg(arg));
    else if (i == 2)
        model->setFilter(QString("id = '%1'").arg(arg));
    else if (i == 3)
        model->setTable("expressinfo");
    else if (i == 4)
        model->setFilter(QString("receiver = '%1'").arg(arg));
    model->select();
    if (!ui->tableView->verticalHeader()->count())
        QMessageBox::warning(this, "提示", "未查询到有效数据");

}

void AdminSYS::addCourier()
{
    QString username = QInputDialog::getText(this, "添加", "要添加的快递员的用户名：");
    QString password = QInputDialog::getText(this, "添加", "为其分配的密码：");
    QString name = QInputDialog::getText(this, "添加", "快递员姓名：");
    QString phone = QInputDialog::getText(this, "添加", "快递员手机号：");
    courier = new Courier();
    courier->setInitInfo(username, password);

    if (courier->signIn())
        QMessageBox::information(this, "提示", "快递员添加成功");
    courier->editInfo(name, phone);
    model2->select();
}

void AdminSYS::deleteCourier()
{
    int row = ui->tableView_2->currentIndex().row();
    QString username = model2->index(row, 0).data().toString();
    //qDebug() << "删除快递员：" << username;
    int ok = QMessageBox::warning(this, "删除数据", "确定删除此快递员?",
             QMessageBox::Yes, QMessageBox::No);
    if (ok == QMessageBox::No)
        return;
    else
        model2->removeRow(row);
//    QSqlQuery query;
//    query.exec(QString("DELETE FROM login WHERE username = '%1'").arg(username));
    client->sendMsg(QString("DELETE FROM login WHERE username = '%1'").arg(username));
    //qDebug() << "快递员登录信息删除成功";

}

void AdminSYS::assignTask()
{
    int row = ui->tableView->currentIndex().row();
    QString status = model->index(row, 6).data().toString();
    if (status == "已签收" || status == "待签收") {
        QMessageBox::warning(this, "错误", "非法操作！");
        return;
    }
    QSqlQuery query;
    QString courierName = QInputDialog::getText(this, "分配任务", "请输入要分配的快递员的用户名：");
    query.exec(QString("SELECT * FROM userinfo WHERE username = '%1'").arg(courierName));
    if (!query.next()) {
        QMessageBox::warning(this, "提示", "没有此快递员！");
        return;
    }

    QString id = model->index(row, 0).data().toString();

//    query.exec(QString("UPDATE expressinfo SET courier = '%1' WHERE id = '%2'")
//               .arg(courierName).arg(id));
    client->sendMsg(QString("UPDATE expressinfo SET courier = '%1' WHERE id = '%2'")
                                   .arg(courierName).arg(id));
    model->select();
    QTimer::singleShot(1000,[=](){
        client->sendMsg("管理员分配快递员#");
    });
}

void AdminSYS::refreshInfo()
{
    model->select();
    model2->select();
    model3->select();
}
